package com.wwt.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2配置类  使用第三方的界面
 */
@Configuration
@EnableSwagger2
public class Swagger2Config  {

    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(createApiInfo())   //apiInfo创建该API的基本信息
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wwt.server.controller")) //选择哪些接口暴露给Swagger
                .paths(PathSelectors.any())  //选择接口下哪些URL产生接口文档
                .build()
                //添加登录认证
                .securitySchemes(getSecuritySchemes())
                .securityContexts(getSecurityContext());
    }

    private ApiInfo createApiInfo() {
        return new ApiInfoBuilder()
                .title("云E办-在线办公系统")  //标题
                .description("迎合时代-筑梦未来！")  //描述
                .version(VERSION)  //版本
                .contact(new Contact("Tao", "www.xxx.xxx", "1521230254@qq.com")) //联系我-作者信息
                .build();
    }

    //通过Swagger2的securitySchemes配置全局参数  即添加哪些参数  在哪添加
    private List<ApiKey> getSecuritySchemes() {
        List<ApiKey> keys = new ArrayList<>();
        // name 为参数名  keyname是页面传值显示的 keyname， name在swagger鉴权中使用
        ApiKey key = new ApiKey("Authorization", "Authorization", "header");
        keys.add(key);
        return keys;
    }

    private List<SecurityContext> getSecurityContext() {
        ArrayList<SecurityContext> list = new ArrayList<>();
        list.add(getContextByPath("/test"));
        list.add(getContextByPath("/admin/info"));
        return list;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex)).build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global",
                "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }

//    //解决静态资源无法访问问题
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/img/**")
//                .addResourceLocations("classpath:/img/");
//    }
}
